package com.zsd.android.okhttplib.chain;

import com.zsd.android.okhttplib.Request;
import com.zsd.android.okhttplib.RequestBody;
import com.zsd.android.okhttplib.Response;
import com.zsd.android.okhttplib.SocketRequestServer;

import java.io.IOException;
import java.util.HashMap;

/**
 * 请求头拦截器
 */
public class RequestHeaderInterceptor implements Interceptor {

    @Override
    public Response doNext(Chain chain) throws IOException {
        //拼接请求头之请求集
        ChainManager manager = (ChainManager) chain;
        Request request = manager.getRequest();
        HashMap<String, String> headerList = manager.getRequest().getHeaderList();
        headerList.put("Host", new SocketRequestServer().getHost(request));
        if (Request.POST.equals(request.getRequestMethod())) {
            //请求体 type lan
            headerList.put("Content-Length", request.getRequestBody().getBody().length() + "");
            headerList.put("Content-Type", RequestBody.TYPE);
        }
        return chain.getResponse(request);//执行下一个拦截器
    }
}
